
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';

import '../../api/ApiUtils.dart';
import '../../api/request/LoginRegisterRequest.dart';
import '../../api/request/UserInfoRequest.dart';
import '../../config/Constant.dart';
import '../../ui/home/HomeTabBar.dart';
import '../../utils/Toast/MyTosat.dart';
import '../../utils/Tool/MyTools.dart';
import '../../utils/userbloc/user_bloc.dart';

class Controller extends GetxController {
  var count = 0.obs;
  RxInt login_ = 0.obs;
  var postion = 0.obs;
  List data = [
    {"name": "郝家伟", "sex": "男", "age": "18"},
    {"name": "杨柳", "sex": "男", "age": "18"},
    {"name": "柏祥", "sex": "男", "age": "18"},
  ].obs;

  increment() {
    count++;
  }

  setPostion(int pos) {
    postion.value = pos;
    update();
  }

  void Login(String email, String password) async {
    login_.value = 1;
    apiShow(
        api: login(email: email, password: generateMD5(password)),
        success: (result) {
          login_.value = 0;
          UserBlocTool.saveAuth(result.data[Constant.Authorization]);
          Info();
          Get.offAll(const HomeTabBar());
        },
        failure: (e) {
          // print("failure$e");
          login_.value = 0;
          MyToast().show("wrong_password".tr,icon_tips: false);
        });
  }

  void Info() {
    if (!MyTools.stringIsEmpty(UserBlocTool.userAuth)) {
      apiShow(
          api: info(),
          success: (result) {
            print(UserBlocTool.getUser()!.toJson());
          },
          failure: (e) {
            // MyToast().show(e.msg);
          });
    }
  }

  void add() async {
    await Future.delayed(const Duration(milliseconds: 300));
    data.add({"name": "杨慧金", "sex": "女", "age": "18"});
  }

  void delete() {
    data.removeAt(0);
  }

  void deleteAll() {
    data.clear();
  }

  void update_() {
    data.insert(0, {"name": "周振东", "sex": "男", "age": "18"});
    update();
  }

  void test() {
    data.insert(data.length - 1, {"name": "杨柳", "sex": "男", "age": "18"});
  }
}
